import requests
import re
import execjs

cookies = {
    'HMACCOUNT': '9F47B428D92F550C',
    'Hm_lvt_9bcbda9cbf86757998a2339a0437208e': '1720709647',
    'no-alert3': 'true',
    'tk': '601100958127505223',
    'sessionid': '1r4gcnrvb338naa18vz7nb8dwuax270r',
    'Hm_lvt_c99546cf032aaa5a679230de9a95c7db': '1720709646,1720793980',
    'm': '9b3590f7b28c919a5a845a151b1c121a|1720952574000',
    'Hm_lpvt_9bcbda9cbf86757998a2339a0437208e': '1720954680',
    'Hm_lpvt_c99546cf032aaa5a679230de9a95c7db': '1720963672',
    'yuanrenxue_cookie': '1720963815|YqbfQfkL8ea4HLqpeU1gJHTYtlbkryWzDnAFFf8BKZ1nfwyjiKlpToYAzaFc78ask3TxASVyAJV9FfhjCR2SWXPWp6CVv36FsITOcmqsscCR78spi',
}

headers = {
    'authority': 'match.yuanrenxue.cn',
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9',
    # 'cookie': 'HMACCOUNT=9F47B428D92F550C; Hm_lvt_9bcbda9cbf86757998a2339a0437208e=1720709647; no-alert3=true; tk=601100958127505223; sessionid=1r4gcnrvb338naa18vz7nb8dwuax270r; Hm_lvt_c99546cf032aaa5a679230de9a95c7db=1720709646,1720793980; m=9b3590f7b28c919a5a845a151b1c121a|1720952574000; Hm_lpvt_9bcbda9cbf86757998a2339a0437208e=1720954680; Hm_lpvt_c99546cf032aaa5a679230de9a95c7db=1720963672; yuanrenxue_cookie=1720963815|YqbfQfkL8ea4HLqpeU1gJHTYtlbkryWzDnAFFf8BKZ1nfwyjiKlpToYAzaFc78ask3TxASVyAJV9FfhjCR2SWXPWp6CVv36FsITOcmqsscCR78spi',
    'referer': 'https://match.yuanrenxue.cn/match/13',
    'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'x-requested-with': 'XMLHttpRequest',
}

def get_cookie():
    response = requests.get('https://match.yuanrenxue.cn/match/13', cookies=cookies, headers=headers, verify=False)
    script = response.text
    script = re.search('<script>(.*)</script>', script)
    js_code =script.group(1)
    js_code = js_code.split("+';path=/'")[0]
    js_code = re.sub('document.cookie', 'cookie', js_code)
    add_code = "; function get_cookie(){return cookie};"
    js_code = js_code + add_code
    ctx = execjs.compile(js_code)
    yuanrenxue = ctx.call('get_cookie').split('=')[1]
    return yuanrenxue

def main():
    rst = 0
    yuanrenxue = get_cookie()
    for i in range(1, 6):

        cookies['yuanrenxue_cookie'] = yuanrenxue
        params = (
            ('page', str(i)),
        )

        response = requests.get('https://match.yuanrenxue.cn/api/match/13', headers=headers, params=params, cookies=cookies, verify=False)
        print(response.text)
        for data in response.json()['data']:
            rst += data['value']
    print(rst)

main()
